package com.wider.foundation.nxfile;

import java.io.File;



public class NxFile {
//spec
	private NxFS filesys;
	private File path;
	private boolean isfolder;
	private long lastmod;
	private long filesize;
	private NxFiles listFiles;
//taiwei add
	private boolean delete;
	private boolean exists;
	private Thread park;

	//Constructors 	
	public NxFile(NxFS fs, File parent, String child){
            filesys=fs;
            path=new File(parent,child);
            park=new Thread();
	}
	public NxFile(NxFS fs, String pathname){
            filesys=fs;
            path=new File(pathname);
            park=new Thread();
	}
	public NxFile(NxFS fs, String parent, String child){
            filesys=fs;
            path=new File(parent,child);
            park=new Thread();
	}
        public NxFile(NxFS fs, File file){
            filesys=fs;
            path=file;
        }

	//public Methods
        //Newly added methods
        public NxFS getFS(){
            return filesys;
        }

	//Spec Methods
	
//	public boolean delete(NxAsynchronous handler){}
	public boolean exists(NxAsynchronous handler){
		FShandler fshandler=new FShandler(handler);
		if(handler != null){
			filesys.getInfo(this, fshandler);
			return exists;
		}
		else{
			filesys.getInfo(this, fshandler);
			if(filesys.isNetworked())
				waitHandler();
			return exists;
		}
	}
	public boolean isDirectory(NxAsynchronous handler){
		FShandler fshandler=new FShandler(handler);
		if(handler != null){
			filesys.getInfo(this, fshandler);
			return isfolder;
		}
		else{
			
			filesys.getInfo(this, fshandler);
			if(filesys.isNetworked())
				waitHandler();
			return isfolder;
		}
		
	}
	public boolean isFile(NxAsynchronous handler){
		FShandler fshandler=new FShandler(handler);
		if(handler != null){
			filesys.getInfo(this, fshandler);
			return !isfolder;
		}
		else{
			filesys.getInfo(this, fshandler);
			if(filesys.isNetworked())
				waitHandler();
			return !isfolder;
		}
	}
	public long lastModified(NxAsynchronous handler){
		FShandler fshandler=new FShandler(handler);
		if(handler != null){
			filesys.getInfo(this, fshandler);
			return lastmod;
		}
		else{
			filesys.getInfo(this, fshandler);
			if(filesys.isNetworked())
				waitHandler();
			return lastmod;
		}
	}
	public long length(NxAsynchronous handler){
		FShandler fshandler=new FShandler(handler);
		if(handler != null){
			filesys.getInfo(this, fshandler);
			return filesize;
		}
		else{
			filesys.getInfo(this, fshandler);
			if(filesys.isNetworked())
				waitHandler();
			return filesize;
		}
	}	
	
	
	
	public NxFiles listFiles(NxAsynchronous handler,NxFileFilter filter){
		FShandler fshandler=new FShandler(handler);
		if(handler != null){
			filesys.list(this, fshandler);
			return listFiles;
		}
		else{
			filesys.list(this, fshandler);
			if(filesys.isNetworked())
				waitHandler();
			return listFiles;
			
		}
	}
	
	//@@William 2010/10/13 : Better to return in "File" type
	//taiwei add
	public File getPath(){
		return path;
	}
        
	public void show(){
		System.out.println("path="+path.toString()+" isfolder="+isfolder+" lastmod="+lastmod
				+" filesize="+filesize+" exists="+exists);
		
	}
	//Methods
	
	private void waitHandler(){
        synchronized(park){
            try {    	
                  park.wait();
          } catch (InterruptedException e) {
                  e.printStackTrace();
          }
        }
	}


	class FShandler implements NxAsynchronous{
		private NxAsynchronous handler;
		public FShandler(NxAsynchronous ihandler){
			handler=ihandler;
		}
		public void NxUpdate(NxFile Npath, boolean Nexists, boolean Nisfolder,	long Nlastmod, long Nfilelength) {
			exists=Nexists;
			filesize=Nfilelength;
			isfolder=Nisfolder;
			lastmod=Nlastmod;
			
			if(handler!=null)
				handler.NxUpdate(Npath, Nexists, Nisfolder, Nlastmod, Nfilelength);
            synchronized(park){
                park.notify();
            }

		}
		
		public void NxUpdate(NxFile path, NxFiles files) {
			listFiles=files;
			//files.show("file");
			if(handler!=null)
				handler.NxUpdate(path,listFiles);
            synchronized(park){
                park.notify();
            }
		}
			
	}




}
